﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using SelfMadeMillioniare.API.Helpers;
using SelfMadeMillioniare.API.TransportObjects;

namespace SelfMadeMillioniare.API.Filters
{
    public class MapExceptionFilter : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext context)
        {
            Response response = ResponseHelper.GetResponse();
            response.AddValidateError(Response.Message, context.Exception);
            context.Response = context.Request.CreateResponse(HttpStatusCode.InternalServerError, response.GetValidateErrors());

            MapActionFilter.AddLogAction(System.Web.HttpContext.Current.Request, context.ActionContext.ControllerContext.ControllerDescriptor.ControllerName, context.ActionContext.ActionDescriptor.ActionName, context.Exception);
        }
    }

    public class MapActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext filterContext)
        {
            AddLogAction(System.Web.HttpContext.Current.Request, filterContext.ControllerContext.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName, null);
            base.OnActionExecuting(filterContext);
        }

        public static void AddLogAction(HttpRequest request, string controller, string action, Exception ex)
        {
            /*int? userId = null;
            string queryString = "";
            string formData = "";

            if (request != null)
            {
                queryString = request.QueryString.ToString();

                StringBuilder sb = new StringBuilder();
                if (request.Form != null)
                {
                    foreach (string key in request.Form.AllKeys)
                    {
                        if (key.StartsWith("__"))
                            continue;

                        if (!string.IsNullOrEmpty(sb.ToString()))
                            sb.Append(" ");
                        sb.Append(key + "=" + request.Form[key].ToString());

                        if (key == "auth_token")
                        {
                            string token = request.Form[key].ToString();
                            using (IUserService service = new UserService())
                            {
                                var user = service.GetByToken(token);
                                if (user != null)
                                    userId = user.Id;
                            }
                        }
                    }

                    formData = sb.ToString();
                }
            }

            using (ILogActionService service = new LogActionService())
            {
                int logActionId = service.AddLogAction(controller, action, userId, queryString, formData, ex);
            }*/

        }

    }

}